package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.SearchHistory;
import com.abaaba.project2.bean.SearchHistoryExample;
import com.abaaba.project2.bean.vo.AdminSearchHistoryVo;
import com.abaaba.project2.mapper.SearchHistoryMapper;
import com.abaaba.project2.service.AdminHistoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminHistoryServiceImpl implements AdminHistoryService {
    @Autowired
    SearchHistoryMapper searchHistoryMapper;

    @Override
    public AdminSearchHistoryVo getHistoryList(Integer page, Integer limit, Integer userId, String keyword, String sort, String order) {
        PageHelper.startPage(page, limit);
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        searchHistoryExample.setOrderByClause(sort + " " + order);

        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (keyword != null && !"".equals(keyword)) {
            criteria.andKeywordLike("%" + keyword + "%");
        }
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }

        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
        PageInfo<SearchHistory> searchHistoryPageInfo = new PageInfo<>(searchHistories);
        long total = searchHistoryPageInfo.getTotal();
        AdminSearchHistoryVo adminSearchHistoryVo = new AdminSearchHistoryVo();
        adminSearchHistoryVo.setItems(searchHistories);
        adminSearchHistoryVo.setTotal(total);
        return adminSearchHistoryVo;
    }
}
